<?php
/**
 * 商城控制器
 */
namespace Admin\Controller;
use Think\Controller;
class ShoppingController extends InitController {
	
	// 商品列表
	public function goodsList(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/goodsList');
	}
	
	// 发布商品
	public function addGoods(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$cate = $this->getCateList();

		$this->assign('cate',$cate);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/addGoods');
	}
	
	// 编辑商品
	public function editGoods(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
	
		$cate = $this->getCateList();
	
		$this->assign('cate',$cate);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/editGoods');
	}
	
	// 编辑商品操作
	public function do_editGoods(){
		
	}
	
	// 删除商品
	public function delGoods(){
		
	}
	
	// 添加商品分类
	public function addCate(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/addCate');
	}
	
	// 添加商品子分类
	public function addSubCate(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$parent_id = I('id',0,'intval');
		
		$cateModel = D('GoodsCate');
		$parentCate = $cateModel->getCateById($parent_id);
		
		$this->assign('parentCate', $parentCate);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/addSubCate');
	}
	
	// 添加商品分类操作
	public function do_addCate(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$name = I('name','','strip_tags,htmlspecialchars,trim');
		if (empty($name)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入分类名称！','field'=>''));
		}
		$alias = I('alias','','strip_tags,htmlspecialchars,trim');
		
		// 判断别名是否包含中文
		if (!isEnglish($alias)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'别名只允许英文数字！','field'=>''));
		}
		
		$cateModel = D('GoodsCate');
		
		// 分类是否存在
		if ($cateModel->getCateByName($name)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该分类名称已经存在！','field'=>''));
		}
		
		// 别名是否存在
		if ($cateModel->getCateByAlias($alias)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该分类别名已经存在！','field'=>''));
		}
		
		$img = I('upimg','','trim');
		$path = '0,';
		$parent_id = I('id',0,'intval');
		
		$sort = I('sort',100,'intval');
		
		$data['name'] = $name;
		$data['alias'] = $alias;
		$data['sort'] = $sort;
		$data['createtime'] = NOW_TIME;
		$data['parent_id'] = $parent_id;
		$data['img'] = $img;
		
		if ($id = $cateModel->add($data)) {
			$data = array();
			if ($parent_id) {
				$_path = $cateModel->where(array('id'=>$parent_id))->getField('path');
				$data['path'] = $_path . ',' . $id;
			} else {
				$data['path'] = '0,' . $id;
			}
			$cateModel->where(array('id'=>$id))->save($data);
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 编辑商品分类
	public function editCate(){
		$id = I('id',0,'intval');
		if (!id) {
			$this->error('参数错误');
		}
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$p = I('p',0,'intval');
		
		$cateInfo = D('GoodsCate')->getCateById($id);
		
		$this->assign('data',$cateInfo);
		$this->assign('p',$p);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/editCate');
	}
	
	// 编辑商品分类操作
	public function do_editCate(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		$name = I('name','','strip_tags,htmlspecialchars,trim');
		if (empty($name)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入分类名称！','field'=>''));
		}
		$alias = I('alias','','strip_tags,htmlspecialchars,trim');
		
		// 判断别名是否包含中文
		if (!isEnglish($alias)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'别名只允许英文数字！','field'=>''));
		}
		
		$cateModel = D('GoodsCate');
		
		// 分类是否存在
		if ($cateModel->getCateByName($name,array('id'=>array('NEQ',$id)))) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该分类名称已经存在！','field'=>''));
		}
		
		// 别名是否存在
		if ($cateModel->getCateByAlias($alias,array('id'=>array('NEQ',$id)))) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该分类别名已经存在！','field'=>''));
		}
		
		$img = I('upimg','','trim');
		$sort = I('sort',100,'intval');
		
		$data['name'] = $name;
		$data['alias'] = $alias;
		$data['sort'] = $sort;
		$data['img'] = $img;
		
		if (false !== $cateModel->where(array('id'=>$id))->save($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 商品分类列表
	public function cateList(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$cateModel = D('GoodsCate');
		$total = $cateModel->getCateNum(array('parent_id'=>0));
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		$list = $cateModel->getCateList(array('parent_id'=>0),'sort desc,id asc',$Page->firstRow.','.$Page->listRows);
		
		// 查询子类
		if(!empty($list)){
			foreach($list as $k=>$v){
				$list[$k]['sub'] = $cateModel->where(array('parent_id'=>$v['id']))->order("sort desc,id asc")->select();
			}
		}
		
		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/cateList');
	}
	
	// 删除商品分类
	public function delCate(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		
		$cateModel = D('GoodsCate');
		
		// 判断是否有子类
		if ($cateModel->hasSubCate($id)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该分类下有子分类，不允许直接删除！','field'=>''));
		}
		
		if ($cateModel->where(array('id'=>$id))->delete()) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 分类
	protected function getCateList(){
		$cateModel = D('GoodsCate');
		$cate = $cateModel->where(array('parent_id'=>0))->order('sort desc,id asc')->select();
		if (!empty($cate)) {
			foreach ($cate as $k=>$v) {
				$cate[$k]['sub'] = $cateModel->where(array('parent_id'=>$v['id']))->order('sort desc,id asc')->select();
			}
		}
		return $cate;
	}
	
	// 添加商品规格
	public function addAttr(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/addAttr');
	}
	
	// 添加商品规格操作
	public function do_addAttr(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$name = I('name','','strip_tags,htmlspecialchars,trim');
		if (empty($name)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入规格名称！','field'=>''));
		}
		
		$attrModel = D('GoodsAttr');
		
		if($attrModel->getAttrByName($name)){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该规格名称已经存在！','field'=>''));
		}
		
		$taxis = I('taxis',100,'intval');
		
		$data['name'] = $name;
		$data['taxis'] = $taxis;
		$data['createtime'] = NOW_TIME;
		
		if ($attrModel->add($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	
	// 商品规格列表
	public function attrList() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$attrModel = D('GoodsAttr');
		$total = $attrModel->getAttrNum();
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		$list = $attrModel->getAttrList('','taxis desc,id asc',$Page->firstRow.','.$Page->listRows);
		
		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/attrList');
	}
	

	
	// 编辑导航
	public function editAttr() {
		$id = I('id',0,'intval');
		if (!id) {
			$this->error('参数错误');
		}
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$p = I('p',0,'intval');
		
		$attrInfo = D('GoodsAttr')->getAttrById($id);
		
		$this->assign('data',$attrInfo);
		$this->assign('p',$p);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Shopping/editAttr');
	}
	
	// 编辑导航操作
	public function do_editAttr() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		
		$name = I('name','','strip_tags,htmlspecialchars,trim');
		if (empty($name)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入规格名称！','field'=>''));
		}
		
		$attrModel = D('GoodsAttr');
		
		if($attrModel->where(array('name'=>$name,'id'=>array('NEQ',$id)))->count()){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'该规格名称已经存在！','field'=>''));
		}
		
		$taxis = I('taxis',100,'intval');
		
		$data['name'] = $name;
		$data['taxis'] = $taxis;
		
		if ($attrModel->where(array('id'=>$id))->save($data) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 删除规格操作
	public function delAttr() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		if (M('GoodsAttr')->where(array('id'=>$id))->delete()) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	
}